package com.suyang.syt.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.suyang.syt.dao.interfaces.UserGroupDao;
import com.suyang.syt.model.UserGroup;

@Repository
public class UserGroupDaoImpl extends BaseDaoImpl<UserGroup> implements UserGroupDao {

	@Override
	public void deleteUserGroups(String userIds) {
		String hql = " delete from UserGroup ug where ug.userId in(" + userIds + ") ";
		this.executeByHql(hql);
	}

	@Override
	public List<UserGroup> listUserGroups(long userId) {
		String hql = " from UserGroup ug where ug.userId=? ";
		return list(hql, userId);
	}

	@Override
	public List<UserGroup> listGroupUsers(long groupId) {
		String hql = " from UserGroup ug where ug.groupId=? order by ug.isAdmin desc";
		return list(hql, groupId);
	}

	@Override
	public void deletes(String ids) {
		String hql = " delete from UserGroup where id in(" + ids + ")";
		this.executeByHql(hql);
	}

	@Override
	public long countUserGroup(long userId, long groupId) {
		String hql = " select count(*) from UserGroup where userId=? and groupId=?";
		return (Long) this.queryByHql(hql, new Object[] { userId, groupId });
	}

	@Override
	public long countUserRole(String groupIds) {
		String hql = " select count(*) from UserGroup where groupId in (" + groupIds + ")";
		return (Long) this.queryByHql(hql);
	}

	@Override
	public void clearUsers(String groupIds) {
		String hql = " delete from UserGroup where groupId in (" + groupIds + ")";
		this.executeByHql(hql);
	}

}